package com.itcast.controller;

import com.itcast.pojo.User;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @PostMapping("/login")
    public Map<String,String> login(@RequestBody User user){
        Map<String,String> map = new HashMap<>();
        try{
            String token = Jwts.builder().setSubject(user.getUserName()) //主题，可以放用户的详细信息
                    .setIssuedAt(new Date()) //token创建时间
                    .setExpiration(new Date(System.currentTimeMillis() + 60000)) //token过期时间
                    .setId("userId") //用户ID
                    .signWith(SignatureAlgorithm.HS256, "liushiyan") //加密方式和加密密码
                    .compact();
            System.out.println("token:"+token);
            map.put("code","1");
            map.put("msg","success");
            map.put("token",token);
            map.put("user",user.getUserName());
        }catch (Exception e){
            map.put("code","0");
            map.put("msg","fail");
            e.printStackTrace();
        }
        return map;
    }

    @PostMapping("/ceshi")
    public void logina(@RequestBody User user){
        System.out.println("a");
    }

}
